#include<iostream>
#include<string>
#include<jsoncpp/json/json.h>
#include"detail.hpp"
#include"message.hpp"

using namespace std;
using namespace Message;

int main()
{
    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(MessType::Mtype ::REQ_RPC);
    // Message::RpcRequest::ptr rrp=Message::MessageFactory::Create<Message::RpcRequest>();
    // rrp->SetMethod("Add");
    // Json::Value params;
    // params["num1"]=11;
    // params["num2"]=22;
    // rrp->SetParams(params);
    // string str=rrp->Serialize();
    // cout<<str<<endl;

    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::RpcRequest::ptr rrp2=dynamic_pointer_cast<Message::RpcRequest>(bmp);
    // cout<<rrp2->Method()<<endl;
    // cout<<rrp2->Params()["num1"].asInt()<<endl;
    // cout<<rrp2->Params()["num2"].asInt()<<endl;

    //测试Topic
    // Message::TopicRequest::ptr trp=Message::MessageFactory::Create<Message::TopicRequest>();
    // trp->SetTopicKey("news");
    // trp->SetTopicMsg("hello world");
    // trp->SetOptype(MessType::TopicOptype::TOPIC_PUBLISH);
    // string str=trp->Serialize();
    // cout<<str<<endl;

    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::REQ_TOPIC);
    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::TopicRequest::ptr trp2=dynamic_pointer_cast<Message::TopicRequest>(bmp);
    // cout<<trp2->GetTopicKey()<<endl;
    // cout<<trp2->GetTopicMsg()<<endl;
    // cout<<(int)trp2->GetOptype()<<endl;

    //测试服务
    // Message::ServiceRequest::ptr srp=Message::MessageFactory::Create<Message::ServiceRequest>();
    // srp->SetMethod("Add");
    // srp->SetServiceOp(MessType::ServiceOptype::SERVICE_DISCOVERY);
    // string str=srp->Serialize();
    // cout<<str<<endl;

    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::REQ_SERVICE);
    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::ServiceRequest::ptr srp2=dynamic_pointer_cast<Message::ServiceRequest>(bmp);
    // cout<<srp2->GetMethod()<<endl;
    // cout<<(int)srp2->GetServiceOp()<<endl;


    // Message::ServiceRequest::ptr srp=Message::MessageFactory::Create<Message::ServiceRequest>();
    // srp->SetMethod("Add");
    // srp->SetServiceOp(MessType::ServiceOptype::SERVICE_REGISTRY);
    // srp->SetHost(Message::Address("127.0.1",9090));
    // string str=srp->Serialize();
    // cout<<str<<endl;

    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::REQ_SERVICE);
    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::ServiceRequest::ptr srp2=dynamic_pointer_cast<Message::ServiceRequest>(bmp);
    // cout<<srp2->GetMethod()<<endl;
    // cout<<(int)srp2->GetServiceOp()<<endl;
    // cout<<srp2->Host().first<<endl;
    // cout<<srp2->Host().second<<endl;

    //测试响应

    // Message::RpcResponse::ptr rsp=Message::MessageFactory::Create<Message::RpcResponse>();
    // rsp->SetRcode(MessType::RCode::RCODE_OK);
    // rsp->SetResult(33);
    // string str=rsp->Serialize();
    // cout<<str<<endl;

    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::RSP_RPC);
    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::RpcResponse::ptr srp2=dynamic_pointer_cast<Message::RpcResponse>(bmp);
    // cout<<srp2->GetResult()<<endl;
    // cout<<(int)srp2->GetRcode()<<endl;


    //测试话题响应
    // Message::TopicResponse::ptr rsp=Message::MessageFactory::Create<Message::TopicResponse>();
    // rsp->SetRcode(MessType::RCode::RCODE_OK);
    // string str=rsp->Serialize();
    // cout<<str<<endl;

    // Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::RSP_TOPIC);
    // bool ret=bmp->Deserialize(str);
    // if(ret==false)
    // {
    //     return -1;
    // }
    // ret=bmp->Check();
    // if(ret==false)  return -1;
    // Message::TopicResponse::ptr srp2=dynamic_pointer_cast<Message::TopicResponse>(bmp);
    // cout<<(int)srp2->GetRcode()<<endl;

    //测试服务响应

    Message::ServiceResponse::ptr rsp=Message::MessageFactory::Create<Message::ServiceResponse>();
    rsp->SetRcode(MessType::RCode::RCODE_OK);
    rsp->SetMethod("Add");
    rsp->SetOptype(MessType::ServiceOptype::SERVICE_DISCOVERY);
    vector<Message::Address> addrs;
    addrs.push_back(Message::Address("127.0.0.1",9090));
    addrs.push_back(Message::Address("127.0.0.2",9091));
    rsp->SetHost(addrs);
    string str=rsp->Serialize();
    cout<<str<<endl;

    Message::BaseMessage::ptr bmp=Message::MessageFactory::Create(Message::Mtype::RSP_SERVICE);
    bool ret=bmp->Deserialize(str);
    if(ret==false)
    {
        return -1;
    }
    ret=bmp->Check();
    if(ret==false)  return -1;
    Message::ServiceResponse::ptr srp2=dynamic_pointer_cast<Message::ServiceResponse>(bmp);
    cout<<(int)srp2->GetRcode()<<endl;
    cout<<(int)srp2->GetOptype()<<endl;
    cout<<srp2->GetMethod()<<endl;
    vector<Message::Address> addr=srp2->GetHosts();
    for(auto &addr:addr)
    {
        cout<<addr.first<<" :"<<addr.second<<endl;
    }


    return 0;
}